package com.user.query.plugin;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.PrintStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;

/*@Component
@Aspect
@Aspect
*/
public class ExceptionAspect {
    private static Properties pro;
    static {
        pro = new Properties();
        InputStream is = ExceptionAspect.class.getClassLoader().getResourceAsStream("system.properties");
        try {
            pro.load(is);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static String getProperty(String key) {
        return pro.getProperty(key);
    }

    /**
     * 只能在程序throw异常的时候方可捕获并进行处理
     * @param joinPoint
     * @return
     * @throws Throwable
     */
    private Object  pointCutMethod(ProceedingJoinPoint joinPoint) throws Throwable {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        Signature signature=joinPoint.getSignature();
        System.out.println( joinPoint.getSignature().getModifiers());
        try {
            Object o=joinPoint.proceed();
            return o;
        }
        catch(Throwable theEnd) {
            SimpleDateFormat sdf2=new SimpleDateFormat("YYYY-MM-dd");
            File file=new File(System.getProperty("user.dir")+"/"+sdf2.format(new Date())+"-error.log");
            PrintStream printStream=null;
            printStream=new PrintStream(new FileOutputStream(file, true));
            printStream.append("\n");
            SimpleDateFormat sdf=new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");
            printStream.append("错误发生时间:"+sdf.format(new Date())+"\n");
            printStream.append("异常发生在："+signature+"\n");
            theEnd.printStackTrace(printStream);
            printStream.append("\n");
            ModelAndView mav = new ModelAndView();
            mav.setViewName("fuwu");
            mav.getModel().put("error", theEnd.getMessage());
            return mav;
        }
    }
    public void afterException(Exception e)  throws Throwable{
      /* System.out.println("异常处理"+e.getMessage());
        File file=new File(pro.getProperty("upload.tempPath")+"\\error.log");
        PrintStream printStream=null;
        printStream=new PrintStream(new FileOutputStream(file, true));
        printStream.append("\n");
        printStream.append("错误发生时间2:"+new Date());
        printStream.append("\n");
        e.printStackTrace(printStream);
        printStream.append("\n");*/
    }

    public void AfterReturning(/*Exception e,*/ JoinPoint jp, Object rtv) {
        /*if (StringUtils.isNotBlank(e.getMessage())) {
            System.out.println("Exception----"+e.getMessage());
        }*/

        System.out.println( jp.getSignature().getName());
        System.out.println( jp.getSignature());
        System.out.println( jp.getArgs()[0]);
        System.out.println( jp.getSourceLocation());
        System.out.println( jp.getTarget());
        System.out.println( jp.getKind());
        System.out.println("method AfterReturning");
    }

}